package util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * AdBottom对象
 * <p>
 * Description. 用于记录到文件的日志对象
 * <p>
 */
public class AdBottomLogUtil {

	public final static Logger clickLogger = LoggerFactory.getLogger("clickinfo");
	public final static Logger impLogger = LoggerFactory.getLogger("impinfo");
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	public static void storeClickLog(HttpServletRequest request, HttpServletResponse response) {
		AdBottomLog adBottomLog = getClickAdBottomFromRequest(request, response);
		clickLogger.info(adBottomLog.toString());
	}

	public static void storeImpLog(HttpServletRequest request, HttpServletResponse response) {
		AdBottomLog adBottomLog = getImpAdBottomFromRequest(request, response);
		impLogger.info(adBottomLog.toString());
	}
	public static void main(String[] args) {
		System.out.println(new AdBottomLog());
	}
	/**
	 * Description. <br>
	 * 从HttpServletRequest,HttpServletResponse中获取Click日志用的AdBottom对象
	 * 
	 * @param request
	 *            请求对象
	 * @param response
	 *            响应对象
	 * @return AdBottom对象
	 */
	public static AdBottomLog getClickAdBottomFromRequest(HttpServletRequest request, HttpServletResponse response) {
		AdBottomLog adBottomLog = new AdBottomLog();
		adBottomLog.setJsType(request.getParameter("jsType"));
		adBottomLog.setMeSiteId(request.getParameter("meSiteId"));
		adBottomLog.setMeSiteType(request.getParameter("meSiteType"));
		adBottomLog.setClCreateTime(formatDateForLog(new Date()));
		adBottomLog.setClExplorer(request.getHeader("User-Agent"));
		adBottomLog.setClUrl(request.getParameter("clUrl"));
		adBottomLog.setClReferUrl(request.getParameter("clReferUrl"));
		adBottomLog.setAdId(request.getParameter("adId"));
		adBottomLog.setAdMaterialId(request.getParameter("adMaterialId"));
		String impTimeString = request.getParameter("impTime");
		if (impTimeString != null) {
			long impTime = 0;
			try {
				impTime = Long.parseLong(impTimeString);
			} catch (NumberFormatException e) {
				System.out.println("传入的impTime字符串不合法" + impTimeString);
			}
			adBottomLog.setImpTime(formatDateForLog(new Date(impTime)));
		}
		return adBottomLog;
		// URL格式
		// /click?jsType=&meSiteId=&meSiteType=&clUrl=&clReferUrl=&adId=&adMaterialId=&impTime=
	}

	/**
	 * Description. <br>
	 * 从HttpServletRequest,HttpServletResponse中获取Imp日志用的AdBottom对象
	 * 
	 * @param requeststastat
	 *            请求对象
	 * @param response
	 *            相应对象
	 * @return AdBottom对象
	 */
	public static AdBottomLog getImpAdBottomFromRequest(HttpServletRequest request, HttpServletResponse response) {
		AdBottomLog adBottomLog = new AdBottomLog();
		adBottomLog.setJsType(request.getParameter("jsType"));
		adBottomLog.setMeSiteId(request.getParameter("meSiteId"));
		adBottomLog.setMeSiteType(request.getParameter("meSiteType"));
		adBottomLog.setClCreateTime(formatDateForLog(new Date()));
		adBottomLog.setClExplorer(request.getHeader("User-Agent"));
		adBottomLog.setClUrl(request.getParameter("clUrl"));
		adBottomLog.setClReferUrl(request.getParameter("clReferUrl"));
		adBottomLog.setAdId(request.getParameter("adId"));
		adBottomLog.setAdMaterialId(request.getParameter("adMaterialId"));
		return adBottomLog;
		// URL格式
		// /imp?jsType=&meSiteId=&meSiteType=&clUrl=&clReferUrl=&adId=&adMaterialId=
	}

	/**
	 * Description.获取日志需要的日期格式 <br>
	 * 日期值是执行时间
	 */
	private static String formatDateForLog(Date date) {
		String dateString = sdf.format(date);
		return dateString.substring(0, dateString.length() - 1);
	}
}
